package com.netstar.ui.activity;

import java.io.File;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.netstar.R;
import com.netstar.model.MtvModel;
import com.netstar.ui.BaseActivity;
import com.netstar.ui.adapter.MtvAdapter;
import com.netstar.util.ConstantSet;
import com.netstar.util.EvtLog;
import com.netstar.util.FileUtil;
import com.netstar.util.ImageUtil;
import com.netstar.util.StringUtil;
import com.netstar.util.ZipUtil;

/***
 * 流量储蓄界面
 * 
 * @author taona
 * 
 */
public class MtvActivity extends BaseActivity implements OnClickListener, OnItemClickListener {

	private static final String TAG = "SaveActivity";
	private static final int NOTIFY_DATA = 1;
	private Button mBtnBack;
	private MtvAdapter mAdapter;
	private ListView mListView;
	private String mFileName;
	private ProgressDialog mProgressDialog;
	private ArrayList<MtvModel> mList;
	private String mUnZipPath;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		EvtLog.d(TAG, "onCreate");
		setContentView(R.layout.mtv_layout);
		initVariables();
		initViews();
		setListener();
		getListData();
	}

	private void setListener() {
		mBtnBack.setOnClickListener(this);
		mListView.setOnItemClickListener(this);
	}

	private void initVariables() {
		mList = new ArrayList<MtvModel>();
		mAdapter = new MtvAdapter(this, mList);
		mUnZipPath = FileUtil.MTV_PATH;
		mFileName = ConstantSet.MTV_ZIP;
	}

	private void initViews() {
		mListView = (ListView) findViewById(R.id.lv_mtv);
		mBtnBack = (Button) findViewById(R.id.btn_title_left);
		mListView.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_title_left:
				finish();
				break;
			default:
				break;
		}
	}

	private void getListData() {
		mProgressDialog = ProgressDialog.show(this, "提示", "正在加载数据....", false, false);
		mList.clear();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					EvtLog.d(TAG, "开始解压" + mUnZipPath);
					String status = get("zip_status");
					if (StringUtil.isNullOrEmpty(status) || !status.equals("ok")) {
						EvtLog.d(TAG, "解压文件：" + FileUtil.FILE_PATH + mFileName);
						EvtLog.d(TAG, "解压文件到路径：" + FileUtil.FILE_PATH + FileUtil.FILE_PATH);
						boolean isSuccess = ZipUtil.dezip(FileUtil.FILE_PATH + mFileName, FileUtil.FILE_PATH);
						if (isSuccess) {
							set("zip_status", "ok");
						} else {
							set("zip_status", "");
						}
					} else {
						EvtLog.d(TAG, "不再解压");
					}

					File f = new File(mUnZipPath);
					File[] files = f.listFiles();
					if (null == files) {
						mHandler.sendEmptyMessage(-1);
						EvtLog.d(TAG, "读取文件失败");
						return;
					}
					EvtLog.d(TAG, "一共有：" + files.length + "个文件");
					for (int i = 0; i < files.length; i++) {
						File file = files[i];
						String fileName = file.getName();
						MtvModel model = new MtvModel();
						model.fileName = FileUtil.getFileNameNoEx(fileName);
						model.file = file;
						model.bitmap = ImageUtil.getVideoThumbnail(file.getAbsolutePath(), 70, 70,
								MediaStore.Images.Thumbnails.MICRO_KIND);
						mList.add(model);
					}
					mHandler.sendEmptyMessage(NOTIFY_DATA);
				} catch (Exception e) {
					mHandler.sendEmptyMessage(-1);
					e.printStackTrace();
				}
			}
		}).start();

	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			mProgressDialog.dismiss();
			if (msg.what == NOTIFY_DATA) {
				if (null != mAdapter) {
					mAdapter.notifyDataSetChanged();
				}
			}
		};
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		FileUtil.openFile(mList.get(arg2).file, this);
	}
}
